/**
 * 绘图层: 用于绘制图形
 * @namespace Note.DrawLayer
 * 
 * Note.DrawLayer类继承自抽象接口
 * interface Layer{
 * 		void load(data);
 * 		array save();
 * 		void clear();
 * }
 * 
 * 构造函数
 * 
 * @constructor DrawLayer
 * @author JunEnstein 2017-05-18
 * @param Object canvas canvas元素对象
 */
Note.DrawLayer = function(canvas, cacheLayer){
	// canvas元素对象
	this._canvas = canvas;
	// 获取绘图句柄
	this._context = canvas.getContext("2d");
	// 初始化绘图对象
	this._painter = new Note.Painter(this._context, cacheLayer);
	// 绘图的路径
	this._paths = [];
	// 绘图类型
	this._shape = null;
	// 是否开始绘制图形
	this._isBegin = false;
	// 绘图开启或关闭标志
	this._enable = true;
};

/**
 * 载入data数据到对象中
 * @method load
 * @author JunEnstein 2017-05-19
 * @param  Object data 对象数据
 */
Note.DrawLayer.prototype.load = function(data){};

/**
 * 保存对象数据
 * @method save
 * @author JunEnstein 2017-05-19
 * @return {paths:[], shape:string} 对象数据
 */
Note.DrawLayer.prototype.save = function(){
	if(this._shape == null) return null;
	var data = {
		paths: this._paths,
		shape: this._shape
	};
	return data;
};

/**
 * 清空信息
 * @method clear
 * @author JunEnstein 2017-05-19
 */
Note.DrawLayer.prototype.clear = function(){
	this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
};

/**
 * 设置绘制形状
 * @method draw
 * @author JunEnstein 2017-05-19
 * @param  string shape 绘图类型
 */
Note.DrawLayer.prototype.draw = function(shape){
	var first = shape.toUpperCase()[0];
	shape = first + shape.toLowerCase().substring(1, shape.length);
	this._shape = shape;
	return shape;
};

/**
 * 开始绘图
 * @method start
 * @author JunEnstein 2017-05-19
 */
Note.DrawLayer.prototype.start = function(){
	if(!this._enable) return ;
	if(this._shape == null) return ;
	this._isBegin = true;
	this._paths = [];
	this.clear();
};

/**
 * 绘图结束
 * @method end
 * @author JunEnstein 2017-05-19
 */
Note.DrawLayer.prototype.end = function(){
	this._isBegin = false;
	this.clear();
};

/**
 * 向绘图路径中追加一个点
 * @method append
 * @author JunEnstein 2017-05-19
 * @param  Point point 点对象(Point{x:int, y:int})
 */
Note.DrawLayer.prototype.append = function(point){
	if(!this._isBegin) return ;
	this._paths.push(point);
};

/**
 * 刷新画面
 * @method refresh
 * @author JunEnstein 2017-05-19
 */
Note.DrawLayer.prototype.refresh = function(){
	if(!this._isBegin) return ;
	this.clear();
	this._painter['draw'+this._shape](this._paths);
};

/**
 * 开启或关闭绘图
 * @method setEnable
 * @author JunEnstein 
 */
Note.DrawLayer.prototype.setEnable = function(enable){
	this._enable = enable;
};

/**
 * 设置画笔样式
 * @method setPainterStyles
 * @author JunEnstein 2017-05-19
 * @param  {Object} styles 画笔样式
 */
Note.DrawLayer.prototype.setPainterStyles = function(styles){
	this._painter.setStyles(styles);
};

/**
 * 设置橡皮样式
 * @method setEraserStyle
 * @author JunEnstein 2017-05-19
 * @param  {Object} styles 画笔样式
 */
Note.DrawLayer.prototype.setEraserStyle = function(styles){
	this._painter.setEraserStyle(styles);
};